function Trim(str) {
	return str;
}
function serialize( mixed_value ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
 
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}function addClose () {
	$('.message').prepend('<span class="close-box"><a href="javascript:;">close</a></span>');
	$('.close-box').click(function () {
		var parent = $(this).parent();
		parent = $(parent).parent();
		$(parent).slideUp();
	});
}
$(document).ready(function() {
	addClose();
	$('.close-box').click(function () {
		var parent = $(this).parent();
		parent = $(parent).parent();
		$(parent).slideUp();
	});
	$('.toggleParent').click(function () {
		var parent = $(this).parent();
		$(parent).slideUp();
	});
	$('fieldset:not(#cms-fieldset-login)').append('<a href="#header" class="back-top">^ back to top</a>');
	$("a.delete-user").click(function() {
		if(document.getElementById('disable-warnings-checkbox').checked === true) {
			var x = $(this).parent().parent();
			$.ajax({
				type: "POST",
				url: "admin.php?page=user-management",
				data: "name="+$(x).children('.username').text(),
				success: function(responseText){
					$('body').append("<div class='floating-box'>"+responseText+"</div>");
					$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
					$(x).fadeOut('slow');
				}		
			});
		}
		else {
			var r = confirm("Are you sure you want to delete this user?");
			if(r === true) {
				var x = $(this).parent().parent();
				$.ajax({
					type: "POST",
					url: "admin.php?page=user-management",
					data: "name="+$(x).children('.username').text(),
					success: function(responseText){
						$('body').append("<div class='floating-box'>"+responseText+"</div>");
						$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
						$(x).fadeOut('slow');
					}		
				});
			}
			else {
				
			}
		}
	});
	$("span.delete-page a").click(function() {
			var r = confirm("Are you sure you want to delete this page? If this is a parent item, ALL SUB-PAGES WILL BE DELETED!");
			if(r === true) {
				var x = $(this).parent().parent();
				$.ajax({
					type: "POST",
					url: "admin.php?page=edit-and-delete-pages",
					data: "deleteid="+$(x).children('.admin-edit-link').attr('id'),
					success: function(responseText){
						$('body').append("<div class='floating-box'>"+responseText+"</div>");
						$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
						$(x).remove();
					}		
				});
			}
			else {
				
			}
	});
	$("a.deleteExternal").click(function(e) {
			var $this = $(this);
			var r = confirm("Are you sure you want to delete this link?");
			if(r === true) {
				var x = $(this).attr('name');
				$.ajax({
					type: "POST",
					url: "admin.php?page=manage-add-external-links",
					data: "deleteid="+x,
					success: function(responseText){
						$('body').append("<div class='floating-box'>"+responseText+"</div>");
						$('.floating-box').hide().fadeIn('slow').animate({opacity: 1}, 3000).fadeOut('slow');
						$this.parent().parent().remove();
					}		
				});
			}
			else {
				
			}
			e.preventDefault();
	});
	$('body').append('<div class="tooltip"></div>');
	$('.helplink').hover(function (e) {
		$(this).data('oldTitle', $(this).attr('title'));
		$(this).attr('title', '');
		$('.tooltip').html($(this).data('oldTitle')).css({top:e.pageY,left:e.pageX}).show();
		$(this).mousemove(function (e) {
			$('.tooltip').css({top:e.pageY,left:e.pageX});
			if (window.innerWidth) { //if browser supports window.innerWidth
				var w = window.innerWidth;
				var h = window.innerHeight;
			}
			else if (document.all) { //else if browser supports document.all (IE 4+)
				var w = document.body.clientWidth;
				var h = document.body.clientHeight;
			}
			var enw =$('.tooltip').css('left').split('px');
			enw = enw[0];
			enw = Number(enw) + 150;
			if (enw > w) {
				var nt = enw - 250;
				$('.tooltip').css('left', nt+'px');
			}
		});
	}, function (e) {
		$('.tooltip').hide();
		$(this).attr('title', $(this).data('oldTitle'));
	});
	$("input").focus(function() {
		// only select if the text has not changed
		$(this).toggleClass('focus');
		if(this.value === this.defaultValue)	{
			this.select();
		}
	});
	$("input, textarea").blur(function() {
		// only select if the text has not changed
		$(this).toggleClass('focus');
	});
	$("input[type^=submit], button").addClass('button');
	$("input[type^=checkbox]").addClass('checkbox');
	$("input, button").hover(function() {
		$(this).toggleClass('hover');
	},
		function () {
			$(this).toggleClass('hover');
		}
	);
	//$("#users").tablesorter( {sortList: [[3,0], [0,0]]} );
	//$("table").tablesorter();
	//$("#username").alphanumeric({nocaps:true});
	$("#theme-name").alphanumeric({allow:' .'});
	$(".alphanumeric").alphanumeric({allow:' '});
	$("table tr").mouseover(function() {
		$(this).addClass("over");
	}).mouseout(function() {
		$(this).removeClass("over");
	});
	/*$("table tr:not(.head) td").mouseover(function() {
		var prev = $(this).prevAll().length;
		var parent = $(this).parents('table');
		$(this).addClass("dark");
		//console.log($(parent).find('tr'));
		//console.log(parent);
		//console.log($('tr td:eq('+prev+')'));
		$(parent).find('tr').each(function (i, domEle) {
			$(domEle).find('td:eq('+prev+')').addClass('dark');
		});
	}).mouseout(function() {
		$('td').removeClass("dark");
	});*/
	$("table tr:even").addClass("alt");
	$(".edit-file").click(function() {
		var href = $(this).attr('title');
		$('.expand-textbox').css('visibility', 'visible');
		$.get('admin.php?page=theme-management&tpage='+href, false, function (responseText) {
			$('.expand-textbox').css('visibility', 'hidden');
			document.getElementById('editTheme').value = responseText;
		});
		
		$('#editing-file-head span').text(href);
		return false;
	});
	$("#formEditTheme").submit(function () {
		$.ajax({
			type: 'POST',
			data: 'tpage='+$('#editing-file-head span').text()+'&content='+encodeURIComponent(document.getElementById('editTheme').value),
			url: 'admin.php?page=theme-management',
			success: function(responseText) {
				$('.msg-box').html(responseText);
				addClose();
			}
		});
		return false;
	});
});